package com.yiwenup.leetcode.offer;

import com.yiwenup.leetcode.TreeNode;

/**
 * https://leetcode-cn.com/problems/ping-heng-er-cha-shu-lcof/
 * 执行用时：1 ms, 在所有 Java 提交中击败了96.74%的用户
 * 内存消耗：38.3 MB, 在所有 Java 提交中击败了80.96%的用户
 **/
public class No055_ii {
    public boolean isBalanced(TreeNode root) {
        if (root == null) return true;
        return (Math.abs(deep(root.left) - deep(root.right)) <= 1) && isBalanced(root.left) && isBalanced(root.right);
    }

    public int deep(TreeNode node) {
        if (node == null) return 0;
        return Math.max(deep(node.left), deep(node.right)) + 1;
    }
}
